package com.beta.cat.config.captcha;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * 图形验证码校验功能。使用方法：<br>
 * <ul>
 *     <li>在需要的应用/项目中，引用当前jar包</li>
 *     <li>在应用的properties文件中，配置com.beta.captcha.enable=true</li>
 *     <li>根据引用的第三方验证码组件，pom文件中引入对应的依赖，如：<br>
 *      <pre>
 *         &lt;!--腾讯天御验证码--&gt;
 *         &lt;dependency&gt;
 *             &lt;groupId&gt;com.tencentcloudapi&lt;/groupId&gt;
 *             &lt;artifactId&gt;tencentcloud-sdk-java&lt;/artifactId&gt;
 *             &lt;version&gt;3.1.322&lt;/version&gt;
 *         &lt;/dependency&gt;
 *      </pre>
 *     </li>
 *     <li>properties或配置中心增加第三方组件中需要配置的参数，以腾讯为例，配置如下：
 * <pre>
 * com.beta.captcha.enable=true
 *
 * com.beta.captcha.secretId=AKIDzqX4QaXIqBJEuLZYHl00nNiqkSyHOIyO
 * com.beta.captcha.secretKey=HK9k8LL6ez72iBRTGpBJEvOfhV7Wg4GE
 * com.beta.captcha.endPoint=captcha.tencentcloudapi.com
 * com.beta.captcha.settings.captchaAppId=2013120966
 * com.beta.captcha.settings.appSecretKey=0qOS_ZbxJ7IvQg6fdccffGg
 * com.beta.captcha.settings.businessId=1
 * com.beta.captcha.settings.sceneId=1
 * </pre>
 *     </li>
 * </ul>
 */
@Configuration
@ConditionalOnProperty(name = "com.beta.captcha.enable", havingValue = "true", matchIfMissing = false)
@ConditionalOnClass(name = "com.tencentcloudapi.common.Credential")
public class CaptchaAutoConfiguration {
    private final Logger log = LoggerFactory.getLogger(CaptchaAutoConfiguration.class);

    @Component
    @ConfigurationProperties(prefix = "com.beta.captcha")
    @RefreshScope
    @Data
    public class CaptchaProperties {
        //密钥id
        private String secretId;
        //密钥key
        private String secretKey;
        //服务器域名，就近地域接入（比如：对于腾讯云来说，上海可以使用captcha.ap-shanghai.tencentcloudapi.com该域名）
        private String endPoint;
        //其它，不同验证码组件的不同配置
        private Properties settings;

        public String getStrSetting(String key) {
            String val = settings.getProperty(key, "");
            return StringUtils.isBlank(val) ? null : val;
        }

        public Integer getIntSetting(String key) {
            String val = settings.getProperty(key, "");
            return StringUtils.isBlank(val) ? null : Integer.parseInt(val.trim());
        }

        public Long getLongSetting(String key) {
            String val = settings.getProperty(key, "");
            return StringUtils.isBlank(val) ? null : Long.parseLong(val.trim());
        }
    }

    public interface CaptchaCheckService {
        /**
         * 验证请求过来的验证码对不对
         *
         * @param captcha 用户输入或操作的 验证码
         * @param noise   干扰码等
         * @return true-验证成功；false-验证失败
         */
        boolean check(String captcha, String noise);
    }

    @Bean
    @ConditionalOnClass(name = "com.tencentcloudapi.common.AbstractClient")
    @ConditionalOnMissingBean
    public CaptchaCheckService createTencentCaptchaService(CaptchaProperties properties) {
        return new TencentCaptchaService(properties);
    }

}
